package com.littlebee.storychild.ui.superFragment;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.AbsListView;

/**
 * 作者：liaoyong on 2016/7/22 13:50
 * 邮箱：liaoyong@tuliu.com
 * 描述：
 */
public abstract class BindListFragment extends BaseFragment {
    public abstract AbsListView getListView();

    public void initNewsListView() {
        if (getListView() == null)
            return;

    }

    private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if (isCancelled()) {
                return null;
            }

            // Simulates a background task
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            baseUpdateHandler.sendEmptyMessage(2000);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
        }

        @Override
        protected void onCancelled() {
            // Notify the loading more operation has finished

        }
    }

    protected abstract int bindRefreshData();

    public abstract void parseJson(Object obj, int arg1);

    public abstract void onDataBinded(int tag);

    protected abstract int bindMoreData();

    /**
     * @Description: 200,response ok;300,parse ok; 1000,refresh data opt; 2000, loadmore opt
     * @author  liaoyong
     * */
    protected Handler baseUpdateHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    parseJson(msg.obj,msg.arg1);
                case 300:
                    onDataBinded(msg.arg1);
                    break;
                case 1000:
                    bindRefreshData();
                    break;
                case 2000:
                    bindMoreData();
                    break;
                default:
                    break;

            }
        }
    };
}
